package com.technology.education.ui.teacher

import android.content.Intent
import android.os.Bundle
import androidx.navigation.Navigation
import androidx.navigation.ui.NavigationUI
import com.technology.education.R
import com.technology.education.data.SpKeys
import com.technology.education.databinding.ActivityMainTeacherBinding
import com.technology.education.ext.*
import com.technology.education.network.apiService
import com.technology.education.ui.common.login.ActivityLogin
import com.technology.education.ui.teacher.upload_answer.UploadAnswerActivity
import me.hgj.jetpackmvvm.base.activity.BaseVmDbActivity
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.lifecycle.KtxActivityManger
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.util.putExtras
import me.hgj.jetpackmvvm.util.startActivity

class MainTeacherActivity : BaseVmDbActivity<BaseViewModel, ActivityMainTeacherBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        initIM()
        getUserInfo()
        getRule()
    }

    private fun getRule() {
        mViewModel.request({ apiService.exchangeRuleTeacher() }, success = {
            getAppViewModel().exchangeRule = it
        })
    }

    private fun getUserInfo() {
        getAppViewModel().getInfo()
    }

    override fun showLoading(message: String) {
    }

    override fun dismissLoading() {
    }

    override fun createObserver() {
        observe(SpKeys.RELOGIN, String::class.java) {
            if (it == "401") {
                KtxActivityManger.finishAllActivity()
                saveData(SpKeys.IS_LOGIN, false)
                startActivity(ActivityLogin::class)
            }
        }

        observe(SpKeys.TEACH_RECORD, String::class.java) {
            if (it.isNotEmpty()) {
                startActivity(
                    Intent(this, UploadAnswerActivity::class.java).putExtras(
                        Pair(
                            "id",
                            it
                        )
                    )
                )
            }
        }
    }

    var currentTime: Long = 0
    override fun onBackPressed() {
        if (fragmentInStackSize() == 1) {
            ///这里做一个判断当前是否在主页面,如果在则提示退出操作
            if (System.currentTimeMillis() - currentTime >= 2 * 1000) {
                toast("再按一次退出应用")
                currentTime = System.currentTimeMillis()
            } else {
                finish()
            }
        } else {

            super.onBackPressed()
        }
    }
}